
package tcpClient;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

import util.MeasurementLog;
import util.SocketManager;

public class Client {
	private SocketManager so;
	private boolean connected = false;
	private boolean logged = false;
	
	public Client(String ip, int port) {
		connected = true;
		try {
			so = new SocketManager(ip, port);
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	public boolean getConnected()
	{
		return connected;
	}
	public boolean getLogged()
	{
		return logged;
	}
	
	public boolean validate(String user, String pass) {
		String reply;
		try {
			System.out.println("validate");
			so.write("USER " + user + "\r\n");
			reply = so.read();
			so.write("PASS " + pass + "\r\n");
			reply = so.read();
			if(reply.startsWith("200"))
			{
				logged = true;
				return true;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return false;
	}
	
	public ArrayList<String> listsensor()
	{
		System.out.println("listsensor");
		ArrayList<String> al = new ArrayList<String>();
		try{
		so.write("LISTSENSOR\r\n");
		String reply = so.read();
		System.out.println(reply);
		while(!reply.startsWith("201"))
		{
			reply = so.read();
			System.out.println(reply);
			al.add(reply);
		}
		for(;!reply.startsWith("201");){
			al.add(reply);			
			reply = so.read();
		}}
		catch (IOException e) {
			e.printStackTrace();
		}
		return al;
	}
	public ArrayList<String> historylog(int sensor_id)
	{
		ArrayList<String> al = new ArrayList<String>();
		try
		{
			so.write("HISTORYLOG " + sensor_id + "\r\n");
			String reply = so.read();
			System.out.println(reply);
			while(!reply.startsWith("212"))
			{
				reply = so.read();
				System.out.println(reply);
				al.add(reply);
				}
				for(;!reply.startsWith("212");){
					al.add(reply);			
					reply = so.read();
				} 
		}
		catch (IOException e) {e.printStackTrace();}
		return al;
	}
	public void onSensor(int sensor_id)
	{
		System.out.println("onSensor");
		try
		{
			so.write("ON " + sensor_id + "\r\n");
			String reply = so.read();
		} 
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	public void offSensor(int sensor_id)
	{
		System.out.println("offSensor");
		try
		{
			so.write("OFF " + sensor_id + "\r\n");
			String reply = so.read();
		} 
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public void gpsOn()
	{
		System.out.println("gpsOn");
		try
		{
			so.write("GPSON\r\n");
			String reply = so.read();
			System.out.println(reply);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	public void gpsOff()
	{
		System.out.println("gpsOff");
		try
		{
			so.write("GPSOFF\r\n");
			String reply = so.read();
			System.out.println(reply);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public String getCurvalue(int sensor_id)
	{
		String reply = null;
		try
		{
			so.write("GET_CURVALUE " + sensor_id + "\r\n");
			reply = so.read();
			System.out.println(reply);
		}
		catch (IOException e) {e.printStackTrace();}
		return reply;
	}
	
	public void getLoc()
	{
		try
		{
			so.write("GET_LOC\r\n");
			String reply = so.read();
			System.out.println(reply);
		}
		catch (IOException e) {e.printStackTrace();}
	}
	
	public void update()
	{
		try
		{
			so.write("UPDATE\r\n");
			String reply = so.read();
			System.out.println(reply);
		}
		catch (IOException e) {e.printStackTrace();}
	}

	
	public boolean quit(){
		try {
			System.out.println("quit");
			so.write("QUIT\r\n");
			String reply = so.read();
			if (reply.startsWith("203"))
				return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}
